package com.tebiecloud.order.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.order.client.model.entity.OrderRefund;
import com.tebiecloud.order.server.service.OrderRefundService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 订单退款信息 前端控制器
 * @author tebie
 * @date 2019-10-07
 */
@RestController
@RequestMapping("/orderrefund")
public class OrderRefundController {

    @Autowired
    private OrderRefundService orderRefundService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<OrderRefund>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        OrderRefund query = pageParams.mapToObject(OrderRefund.class);
        QueryWrapper<OrderRefund> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(orderRefundService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<OrderRefund> get(@RequestParam("id") Long id) {
        OrderRefund entity = orderRefundService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param orderId 订单id,
     * @param type 退款类型：1用户发起退款，2商户发起退款,
     * @param inHand 收货状态，0未收货1已收货,
     * @param reason 退货退款原因,
     * @param status 状态，0没有退成功1退款审核中2等待退货3退货中4已退货退款,
     * @param explainn 退货退款说明,
     * @param receiver 收货人名称,
     * @param address 收货地址,
     * @param phone 收货人联系号码,
     * @param logistics 物流公司,
     * @param courierNumber 快递单号,
     * @param senderPhone 寄出人联系号码,
     * @param refuseReason 申请退货退款失败原因
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "orderId", required = false) Long orderId,
        @RequestParam(value = "type", required = false) Integer type,
        @RequestParam(value = "inHand", required = false) Integer inHand,
        @RequestParam(value = "reason", required = false) String reason,
        @RequestParam(value = "status", required = false) Integer status,
        @RequestParam(value = "explainn", required = false) String explainn,
        @RequestParam(value = "receiver", required = false) String receiver,
        @RequestParam(value = "address", required = false) String address,
        @RequestParam(value = "phone", required = false) String phone,
        @RequestParam(value = "logistics", required = false) String logistics,
        @RequestParam(value = "courierNumber", required = false) String courierNumber,
        @RequestParam(value = "senderPhone", required = false) String senderPhone,
        @RequestParam(value = "refuseReason", required = false) String refuseReason
    ) {
        OrderRefund entity = new OrderRefund();
        entity.setOrderId(orderId);
        entity.setType(type);
        entity.setInHand(inHand);
        entity.setReason(reason);
        entity.setStatus(status);
        entity.setExplainn(explainn);
        entity.setReceiver(receiver);
        entity.setAddress(address);
        entity.setPhone(phone);
        entity.setLogistics(logistics);
        entity.setCourierNumber(courierNumber);
        entity.setSenderPhone(senderPhone);
        entity.setRefuseReason(refuseReason);
        orderRefundService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param orderId 订单id
     * @param type 退款类型：1用户发起退款，2商户发起退款
     * @param inHand 收货状态，0未收货1已收货
     * @param reason 退货退款原因
     * @param status 状态，0没有退成功1退款审核中2等待退货3退货中4已退货退款
     * @param explainn 退货退款说明
     * @param receiver 收货人名称
     * @param address 收货地址
     * @param phone 收货人联系号码
     * @param logistics 物流公司
     * @param courierNumber 快递单号
     * @param senderPhone 寄出人联系号码
     * @param refuseReason 申请退货退款失败原因
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "orderRefundId", required = false) Long orderRefundId,
            @RequestParam(value = "orderId", required = false) Long orderId,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "inHand", required = false) Integer inHand,
            @RequestParam(value = "reason", required = false) String reason,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "explainn", required = false) String explainn,
            @RequestParam(value = "receiver", required = false) String receiver,
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "logistics", required = false) String logistics,
            @RequestParam(value = "courierNumber", required = false) String courierNumber,
            @RequestParam(value = "senderPhone", required = false) String senderPhone,
            @RequestParam(value = "refuseReason", required = false) String refuseReason
    ) {
        OrderRefund entity = new OrderRefund();
        entity.setOrderRefundId(orderRefundId);
        entity.setOrderId(orderId);
        entity.setType(type);
        entity.setInHand(inHand);
        entity.setReason(reason);
        entity.setStatus(status);
        entity.setExplainn(explainn);
        entity.setReceiver(receiver);
        entity.setAddress(address);
        entity.setPhone(phone);
        entity.setLogistics(logistics);
        entity.setCourierNumber(courierNumber);
        entity.setSenderPhone(senderPhone);
        entity.setRefuseReason(refuseReason);
        orderRefundService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            orderRefundService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            orderRefundService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
